﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using VehicleManagement.Models;
using VehicleManagement.Codes;

namespace VehicleManagement.ControllersServices
{
    public class CarInfoServices
    {
        /// <summary>
        /// 获取所有车辆
        /// </summary>
        /// <returns>IList<CarInfo></returns>
        public IList<CarInfo> GetAllCarsInfo()
        {
            IList<CarInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SELECT_CARINFO", null);
            return lstModel;
        }

        /// <summary>
        /// 根据条件搜索所有的车辆
        /// </summary>
        /// <param name="ht">搜索哈希表</param>
        /// <returns>IList<CarInfo></returns>
        public IList<CarInfo> SearchAllCar(Hashtable ht)
        {
            IList<CarInfo> lstCar = new List<CarInfo> { };
            lstCar = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SEARCH_CAR", ht);
            return lstCar;
        }

        /// <summary>
        /// 根据车辆ID搜索车辆
        /// </summary>
        /// <param name="id">车辆ID</param>
        /// <returns>车辆对象</returns>
        public CarInfo GetCarInfoByID(int id)
        {
            IList<CarInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SELECT_CARINFO_BY_ID", id);
            CarInfo carinfo = lstModel.Where(u => u.CarID == id).First();
            return carinfo;
        }


        /// <summary>
        /// 根据车牌号查找车辆
        /// </summary>
        /// <param name="number"></param>
        /// <returns>车辆对象</returns>
        public CarInfo GetCarsByNumber(string number)
        {
            CarInfo carInfo = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SELECT_CAR_BY_NUM", number).First();
            return carInfo;
        }
        /// <summary>
        /// 根据车辆是否可用进行筛选
        /// </summary>
        /// <param name="avialable"></param>
        /// <returns></returns>
        public IList<CarInfo> GetCarsIsAvailable(int avialable)
        {
            IList<CarInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SELECT_CARINFO_BY_AVAILABLE", avialable);
            return lstModel;
        }

        /// <summary>
        /// 根据车辆类型选折车辆
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<CarInfo> GetCarsByType(string type)
        {
            IList<CarInfo> listCarInfo = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SELECT_CAR_BY_TYPE", type);
            return listCarInfo;


        }
        /// <summary>
        /// 根据CarId查找CarNum
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetCarNumByCarId(int id)
        {
            string carNumber = "";
            return carNumber = SqlMapperManager.Instance.Get().QueryForObject("SELECT_CARNUM_BY_ID", id).ToString();
 
        }


        /// <summary>
        /// 根据CarNum查找CarId
        /// </summary>
        /// <param name="carNum"></param>
        /// <returns></returns>
        public int GetCarIdByCarNum(string carNum)
        {
            int carID = 0;
            return carID = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_CARID_BY_CarNum", carNum));
 
        }

        /// <summary>
        /// 查找当前状态可用的车辆的车牌号
        /// </summary>
        /// <returns>CarNum的IList<string></returns>
        public IList<string> GetAllCarNumByAvailable()
        {
            IList<string> listCarNum=SqlMapperManager.Instance.Get().QueryForList<string>("SELECT_CARNUM_BY_AVAILABLE",1);
            return listCarNum;
 
        }


        /// <summary>
        /// 插入新车辆信息
        /// </summary>
        /// <param name="driver"></param>
        /// <returns></returns>
        public object CreateCar(CarInfo carinfo)
        {
            return SqlMapperManager.Instance.Get().Insert("INSERT_CAR", carinfo);
        }

        /// <summary>
        /// 修改车辆信息
        /// </summary>
        /// <param name="driver"></param>
        /// <returns></returns>
        public int EditCar(CarInfo carinfo)
        {
            int row = SqlMapperManager.Instance.Get().Update("UPDATE_CARINFO", carinfo);
            return row;
        }
        /// <summary>
        /// 删除车辆
        /// </summary>
        /// <param name="id">车辆的ID</param>
        /// <returns></returns>
        public int DeleteCar(int id)
        {
            int i = SqlMapperManager.Instance.Get().Delete("DELETE_CARINFO", id);
            return i;
        }



        public int CountAllCar(Hashtable ht)
        {
            return SearchAllCar(ht).Count;
        }

        public IList<CarInfo> SearchAllCar(Hashtable ht, int skip, int max)
        {
            IList<CarInfo> lstCar = SqlMapperManager.Instance.Get().QueryForList<CarInfo>("SEARCH_CAR", ht,skip,max);
            return lstCar;
        }



        /// <summary>
        /// 根据车辆的ID，修改其目前的状态，返回1修改成功
        /// </summary>
        /// <returns>返回1时，修改成功，返回-1说明修改失败</returns>
        public int UpdateCarState(int carID, int state)
        {
            int Car_available = -1;
            Hashtable hastable = new Hashtable();
            if (state >= 0)
            {
                if (state == 1)
                {
                    Car_available = 1;
                }
                if (state == 0)
                {
                    Car_available = 0;
                }
                hastable.Add("CarState", Car_available);
                hastable.Add("CarID", carID);
                int ok = SqlMapperManager.Instance.Get().Update("UPDATE_CARSTATE", hastable);
                return ok;
            }
            else
            {
                return -1;
            }





        }
    }
}